package com.lcf.app.swagger.config;

import io.swagger.annotations.Api;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.server.WebServer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;


/**
 * @author ChenFei
 * @date 2021/11/3
 */
@Slf4j
@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "swagger.config")
@EnableSwagger2
public class Knife4jConfiguration implements ApplicationListener<WebServerInitializedEvent> {

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("knife4j-api-文档")
                        .description("文档描述")
                        .termsOfServiceUrl("www.xxx.com")
                        .contact(new Contact("ChenFei", null, null))
                        .version("v1.0.0")
                        .build())
                //分组名称
                .groupName("分组")
                //全局请求参数
                .globalRequestParameters(requestParameterList())
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 设置全局请求参数
     *
     * @return
     */
    private List<RequestParameter> requestParameterList() {
        List<RequestParameter> requestParameterList = new ArrayList<>();
        RequestParameter parameter = new RequestParameterBuilder()
                .name("access_token")
                .required(false)
                .in(ParameterType.HEADER)
                .build();
        requestParameterList.add(parameter);
        return requestParameterList;
    }

    @SneakyThrows
    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        WebServer server = event.getWebServer();
        WebServerApplicationContext context = event.getApplicationContext();
        Environment env = context.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        int port = server.getPort();
        String contextPath = env.getProperty("server.servlet.context-path");
        if (contextPath == null) {
            contextPath = "/";
        }
        log.info("项目启动成功：http://{}:{}{}doc.html", ip, port, contextPath);
    }
}
